var mahees = {
	backbone : {
		EventBus : function() {
			return _.extend({}, Backbone.Events);
		},
		Model : {},
		Collection : {},
		View : {},
		Router : {},
		Utility : {}
	}
};

// Generic stuff
_.templateSettings = {
	interpolate : /\<\?\=(.+?)\?\>/g
};

mahees.backbone.View.BasicView = Backbone.View.extend({

	initialize : function() {
		_.bindAll(this, 'render', 'unrender', 'remove', 'updateModel', 'errorHandler');

		this.model.bind('change', this.render);
		this.model.bind('remove', this.unrender);
		this.model.bind('error', this.errorHandler);

		if (this.customInitialize) {
			this.customInitialize();
		}
	},

	updateModel : function(e) {
		mahees.backbone.Utility.FieldChanged(this.model, e, this.el);
	},

	errorHandler : function(model, errorObj) {
		mahees.backbone.Utility.ErrorHandler('.' + this.viewIdentifier, errorObj, this.el);
	},

	unrender : function() {
		$(this.el).remove();
	},

	remove : function() {
		this.model.destroy();
	},

	render : function() {
		var templateId = '#' + this.viewIdentifier + 'Template';
		var template = _.template($(templateId).html())
		$(this.el).html(template(this.model.toJSON()));
		if (this.customRender) {
			this.customRender();
		}
		return this;
	},
});

mahees.backbone.View.BasicListView = Backbone.View.extend({
	initialize : function() {
		_.bindAll(this, 'render', 'appendItem');

		this.collection.bind('reset', this.render);
		this.collection.bind('add', this.appendItem);

		this.counter = 0;
	},

	render : function() {
		$(this.el).empty();
		this.counter = 0;

		_(this.collection.models).each(function(item) {
			this.appendItem(item);
		}, this);

		return this;
	},

	appendItem : function(item) {
		this.counter++;
		item.set({
			counter : this.counter
		}, {
			silent : true
		});
		var view = new this.refModelView({
			model : item,
			eventBus : this.options.eventBus
		});
		$(this.el).append(view.render().el);
	}
});
// End generic stuff

//Utilities
mahees.backbone.Utility.FieldChanged = function(model, e, el) {
	var obj = {};
	var searchStr = "[name='" + e.target.name + "']";
	obj[e.target.name] = $(searchStr, el).val();
	model.set(obj);
}

mahees.backbone.Utility.ErrorHandler = function(className, errorObj, el) {
	var searchStr = className + "[name='" + errorObj.name + "']";
	var node = $(searchStr, el);
	node.attr('style', "border: 2px solid red;");
	node.focus();
}

mahees.backbone.Utility.ResetCollection = function(model, attr) {
	var collection = model.previous(attr)
	collection.reset(_.map(model.get(attr), function(jsonObj) {
		return new collection.model(jsonObj)
	}));

	var setObj = {};
	setObj[attr] = collection;

	model.set(setObj, {
		silent : true
	});
}

mahees.backbone.Utility.ConvertArrayToCollection = function(jsonObject, attr, refCollFunc) {
	var collection = new refCollFunc();
	collection.reset(_.map(jsonObject[attr], function(jsonObj) {
		return new collection.model(jsonObj)
	}));

	jsonObject[attr] = collection;
}

mahees.backbone.Utility.SelectItem = function(model, attr, el) {
	$("select[name='" + attr + "']", el).val(model.get(attr));
}

mahees.backbone.Utility.Validator = function() {

	return {
		email : function(value) {
			var reg = /^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/;
			return reg.test(value);
		},

		phone : function(value) {
			var reg = /^\d{10}$/;
			return reg.test(value);
		},

		zip : function(value) {
			var reg = /^\d{5}$/;
			return reg.test(value);
		},

		empty : function(value) {
			return !value || value.length < 1;
		},

		min : function(value, min) {
			return !value || value > min;
		},

		max : function(value, max) {
			return !value || value < max;
		},

		eq : function(value, eq) {
			return !value || value == eq;
		}
	};

}
